package com.mg.admin.controller.admin;


import com.mg.admin.bean.admin.*;
import com.mg.admin.constants.Constants;
import com.mg.admin.controller.BaseController;
import com.mg.admin.service.admin.OrganizationService;
import com.mg.admin.service.admin.ResourceService;
import com.mg.admin.service.admin.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/admin/role/")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private OrganizationService organizationService;

    @RequestMapping(value= LIST,method = RequestMethod.GET)
    public void list(ModelMap modelMap){
        UserVO userVO = currentUser();
        Long orgId = userVO.getOrgId();
        if (orgId == null) orgId = (long)Constants.ONE;
        List<OrganizationVO> voList = organizationService.findChildByParentId(orgId);
        modelMap.put("list",voList);
        modelMap.put("orgId",orgId);
    }

    @RequestMapping(value= ADD,method = RequestMethod.GET)
    public void add(ModelMap modelMap, Long orgId){
        UserVO userVO = currentUser();
        String resourceId = userVO.getResourceIds();
        List<ResourceVO> voList = resourceService.findById(resourceId);
        modelMap.put("list",voList);
        modelMap.put("orgId",orgId);
    }

    @GetMapping(EDIT)
    public void edit(ModelMap modelMap,Long id){
        UserVO userVO = currentUser();
        String resourceId = userVO.getResourceIds();
        RoleVO roleVO = roleService.findById(id);
        List<ResourceVO> voList = resourceService.findById(resourceId);
        modelMap.put("bean",roleVO);
        modelMap.put("list",voList);
    }

    @GetMapping(DELETE)
    @ResponseBody
    public void delete(Long id){
        roleService.delete(id);
    }

    @GetMapping(VIEW)
    public void view(ModelMap modelMap,Long id){
        RoleVO roleVO = roleService.findById(id);
        List<ResourceVO> voList = resourceService.findById(roleVO.getResourceIds());
        StringBuilder sb = new StringBuilder();
        for (ResourceVO vo : voList){
            sb.append(vo.getName() + ",");
        }
        roleVO.setResourceIds(sb.toString().substring(0,sb.length()-1));
        modelMap.put("bean",roleVO);
    }



    @ResponseBody
    @RequestMapping(value= SAVE,method = RequestMethod.POST)
    public void save(RoleVO roleVO){
        if (roleVO.getId() == null){
            roleService.insertOne(roleVO);
        }else {
            roleService.updateOne(roleVO);
        }
    }


    @ResponseBody
    @RequestMapping(value = LIST,method = RequestMethod.POST)
    public Result<List<RoleVO>> pageList(Long orgId){
        if (orgId == null) orgId = currentUser().getOrgId();
       List<RoleVO> voList = roleService.findAllByOrgId(orgId);
        return new Result<>(voList);
    }

}